package com.tbit.uqbike.webmanager.controller;

import com.tbit.common.constant.MethodEnum;
import com.tbit.common.constant.ModuleEnum;
import com.tbit.common.entity.view.Result;
import com.tbit.uqbike.annos.OperateLog;
import com.tbit.uqbike.constant.UserConstant;
import com.tbit.uqbike.object.pojo.dto.ProtocolDTO;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.uqbike.service.business.ProtocolService;
import com.tbit.uqbike.webmanager.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Objects;

/**
 * 协议自定义
 *
 * @author <a href="mailto:liu.haoxiang@tbit.com.cn">BUGXiang</a>
 * @version 1.0
 * @since 2025-4-7
 */
@RestController
@RequestMapping("/protocol")
public class ProtocolController {


    @Resource
    private ProtocolService protocolService;


    /**
     * 获取协议内容
     *
     * @param brandId 品牌id
     * @param type    类型 0-使用协议 1-隐私协议
     * @return Result
     */
    @GetMapping("/getProtocol")
    public Result<Object> getProtocol(Integer brandId, Integer type, HttpServletRequest request) {
        Assert.notNull(brandId, "品牌不能为空");
        Assert.notNull(type, "品牌不能为空");
        LoginInfo login = BaseController.getLogin(request);
        if (Objects.equals(login.getType(), UserConstant.USER_BRAND) || login.roleIsAdmin()){
            return protocolService.getProtocol(brandId, type);
        }
        return Result.error("无权限");
    }


    /**
     * 编辑协议
     * @return Result
     */
    @PostMapping("/editProtocol")
    @OperateLog(module = ModuleEnum.PROTOCOL, methods = MethodEnum.PROTOCOL_EDIT)
    public Result<Object> editProtocol(@RequestBody @Valid ProtocolDTO dto, HttpServletRequest request) {
        LoginInfo login = BaseController.getLogin(request);
        if (Objects.equals(login.getType(), UserConstant.USER_BRAND) || login.roleIsAdmin()){
            return protocolService.editProtocol(dto.getBrandId(), dto.getType(), dto.getContent());
        }
        return Result.error("无权限操作");
    }
}
